package com.webbuilder.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class WeatherUtil {
	
    public static List<Map<String,Object>> getWeather(String cityCode) {
    	   URL url=null;
    	   URLConnection connectionData=null;
    	   BufferedReader br=null;
    	   StringBuilder sb = null; 
    	   Map<String,Object> wetherMap;
			try {
				url = new URL("http://m.weather.com.cn/atad/" + cityCode + ".html");
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block atad
				e.printStackTrace();
			}
		
    	  
		
			try {
				connectionData = url.openConnection();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
           connectionData.setConnectTimeout(1000); 
           System.out.println(connectionData);
           
           
        	   
			
				try {
					br = new BufferedReader(new InputStreamReader( 
					           connectionData.getInputStream(), "UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			
               System.out.println(br);
                sb = new StringBuilder(); 
               String line = null; 
               try {
				while ((line = br.readLine()) != null) 
				       sb.append(line);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
          
               String datas = sb.toString();   
               String infos=JsonUtil.getMap(datas).get("weatherinfo").toString(); 
               Map info = JsonUtil.getMap(infos);
               // jsonData = JSONObject.fromObject(datas); 
             //  System.out.println(jsonData.toString());  
              // info = jsonData.getJSONObject("weatherinfo"); 
           
           //得到1到6天的天气情况
           List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
           for(int i=1;i<=6;i++){
           	//得到未来6天的日期
           	Calendar cal = Calendar.getInstance();
           	cal.add(Calendar.DAY_OF_YEAR, i-1);
           	Date date = cal.getTime();
           	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
           	
           	Map<String,Object> map = new HashMap<String, Object>();
           	map.put("city", info.get("city").toString());//城市
           	map.put("date_y", sf.format(date));//日期
           	map.put("week", getWeek(cal.get(Calendar.DAY_OF_WEEK)));//星期
           	map.put("fchh", info.get("fchh").toString());//发布时间
           	map.put("weather", info.get("weather"+i).toString());//天气
           	map.put("temp", info.get("temp"+i).toString());//温度
           	map.put("wind", info.get("wind"+i).toString());//风况
           	map.put("fl", info.get("fl"+i).toString());//风速
           	map.put("index", info.get("index").toString());// 今天的穿衣指数 
           	map.put("index_uv", info.get("index_uv").toString());// 紫外指数 
           	map.put("index_tr", info.get("index_tr").toString());// 旅游指数 
           	map.put("index_co", info.get("index_co").toString());// 舒适指数 
           	map.put("index_cl", info.get("index_cl").toString());// 晨练指数 
           	map.put("index_xc", info.get("index_xc").toString());//洗车指数 
           	map.put("index_d", info.get("index_d").toString());//天气详细穿衣指数 
           	list.add(map);
           }
           //控制台打印出天气
           wetherMap=list.get(0);
          for(int j=0;j<list.size();j++){
       	   Map<String,Object> wMap = list.get(j);
       	   System.out.println(wMap.get("city")+"\t"+wMap.get("date_y")+"\t"+wMap.get("week")+"\t"
       			   +wMap.get("weather")+"\t"+wMap.get("temp")+"\t"+wMap.get("index_uv"));
          }
    	return list;
    }
    private static String getWeek(int iw){
    	String weekStr = "";
    	switch (iw) {
		case 1:weekStr = "星期天";break;
		case 2:weekStr = "星期一";break;
		case 3:weekStr = "星期二";break;
		case 4:weekStr = "星期三";break;
		case 5:weekStr = "星期四";break;
		case 6:weekStr = "星期五";break;
		case 7:weekStr = "星期六";break;
		default:
			break;
		}
    	return weekStr;
    }
}
